package de.fhb.itunes.manager.mail;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;

import de.fhb.itunes.manager.Config;

public class Helper_File {
	public static String ladeDatei(String datName) {
		Config config = new Config();
		Properties prop = config.getProperties();
		String realpath = prop.getProperty("abs_path");
		File file = new File(realpath+datName);

		if (!file.canRead() || !file.isFile()) {
			System.out.println("kann die datei "+realpath + datName+" nicht finden");
			return null;
		} else {
			FileReader fr = null;
			int c;
			StringBuffer buff = new StringBuffer();
			try {
				fr = new FileReader(file);
				while ((c = fr.read()) != -1) {
					buff.append((char) c);
				}
				fr.close();

			} catch (IOException e) {
				e.printStackTrace();
			}

			//System.out.println(buff.toString());
			return buff.toString();
		}
	}
	public static void speichereInDatei(String inhalt){
		Config config = new Config();
		Properties prop = config.getProperties();
		String realpath = prop.getProperty("abs_path");
		File outputFile = new File(realpath+"mails//mail.html");

		try {
			outputFile.createNewFile();
		} catch (IOException e1) {
			System.out.println("Fehler beim erstellen der datei");
			e1.printStackTrace();
		}

		try {
			outputFile.createNewFile();

			PrintWriter output;
			BufferedWriter buffer;
			FileWriter fileWriter;
		    fileWriter = new FileWriter(outputFile);
		    buffer = new BufferedWriter(fileWriter);
		    output = new PrintWriter(buffer);
		    try {
		        output.print(inhalt);
		    } finally {
		        output.close();
		        buffer.close();
		        fileWriter.close();
		    }
		} catch (IOException e) {
		    System.out.println("Fehler");
		    e.printStackTrace();
		}
	}
}
